﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mandelbrot_Florian.Model;
using Mandelbrot_Florian.View;
using Mandelbrot_Florian.Service;
using System.Drawing;
using System.Threading;

namespace Mandelbrot_Florian.Controller
{
    public class MainController : IDisposable
    {
        #region Attribut

        public MandelbrotModel CurrentModel;
        public MainForm view;
        public MandelbrotService Service;

        #endregion

        public MainController()
        {
            view = new MainForm(this);
            CurrentModel = new MandelbrotModel();
            Service = new MandelbrotService(this);
        }

        public void UpdateModel(decimal Sx, decimal Sy, decimal Fx, decimal Fy, decimal Maxiterations, string colorName, int ImgWidth, int ImgHeight)
        {
            CurrentModel.Sx = (double)Sx;
            CurrentModel.Sy = (double)Sy;
            CurrentModel.Fx = (double)Fx;
            CurrentModel.Fy = (double)Fy;
            CurrentModel.MaxIteration = (int)Maxiterations;
            CurrentModel.ColorName = colorName;
            CurrentModel.Colors = MandelbrotService.GetColors(colorName);
            CurrentModel.Img = new Bitmap(ImgWidth, ImgHeight);
        }
        public void UpdateLabels(MandelbrotModel model = null)
        {
            if (model == null)
                model = CurrentModel;
            view.Invoke(new Action(() =>
            {
                view.ntxtSx.Value = (decimal)model.Sx;
                view.ntxtSy.Value = (decimal)model.Sy;
                view.ntxtFx.Value = (decimal)model.Fx;
                view.ntxtFy.Value = (decimal)model.Fy;
                view.ntxtIteration.Value = model.MaxIteration;
                view.lbTime.Text = String.Format("{0}.{1} s", model.Timer.Elapsed.Seconds, model.Timer.Elapsed.Milliseconds);
                view.lbImgSize.Text = "(" + model.Img.Width + ", " + model.Img.Height + ")";
                view.cbColors.SelectedItem = model.ColorName;
            }));
        }

        public void InitProgressBar()
        {
            view.Invoke(new Action(() =>
            {
                view.progressBar.Value = 0;
                view.progressBar.Maximum = view.imgMandelbrot.Width;
                view.progressBar.Visible = false;
            }));
        }
        public void UpdateProgress()
        {
            view.Invoke(new Action(() =>
            {
                view.progressBar.Visible = true;
                view.progressBar.Value++;
                //if finish
                if (view.progressBar.Value == (view.progressBar.Maximum - 1))
                    view.progressBar.Visible = false;
            }));
        }
        public void UpdateImg()
        {
            view.Invoke(new Action(() =>
            {
                view.imgMandelbrot.Image = CurrentModel.Img;
            }));
        }

        public void DrawMandel()
        {
            view.btnDraw.Enabled = false;
            view.btnReset.Enabled = false;
            view.btnSave.Enabled = false;
            UpdateModel(view.ntxtSx.Value, view.ntxtSy.Value, view.ntxtFx.Value, view.ntxtFy.Value, view.ntxtIteration.Value, view.cbColors.SelectedItem.ToString(), view.imgMandelbrot.Width, view.imgMandelbrot.Height);
            if (view.rbNo.Checked)
            {
                Service.DrawMandel();
            }
            else if (view.rbTheard.Checked)
            {
                Service.DrawMandelByThread();
            }
            else if (view.rbTask.Checked)
            {
                Service.DrawMandelByTask();
            }
            else if (view.rbTask2.Checked)
            {
                Service.DrawMandelByTask2();
            }
        }
        public void EndingDrawMandel(MandelbrotModel model)
        {
            CurrentModel = model;
            UpdateLabels();
            UpdateImg();
            view.Invoke(new Action(() =>
            {
                view.btnDraw.Enabled = true;
                view.btnReset.Enabled = true;
                view.btnSave.Enabled = true;
            }));
        }
        public void SaveImgMandel(string fileName)
        {
            CurrentModel.Img.Save(fileName);
        }

        public void Dispose()
        {
            view.Dispose();
        }
    }
}
